package com.hotelvip.common.scheduled;

import com.hotelvip.common.annotation.VipUserAnno;
import com.hotelvip.entity.VipUser;
import com.hotelvip.service.VipUserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;

/**
 * @author Lzh
 * @date 2021/1/20 13:59
 */
@Component
@EnableScheduling
@Slf4j
public class TimeTask {
    @Autowired
    private VipUserService vipUserService;


    //定期修改过期的会员卡状态
    @Scheduled(cron = "0 0 2 * * ?")
//    @Scheduled(cron = "0 * * * * ?")
    @VipUserAnno(key = "updateStatus")
    public List<VipUser> changeVipStatus(){
        log.info("定时修改状态任务:{}",new Date());
        List<VipUser> vipUsers = vipUserService.list();
        for (VipUser vipUser : vipUsers) {
            if (vipUser.getEndDate().getTime()-System.currentTimeMillis()<=0){
                vipUser.setStatus(4);
            }
        }
        vipUserService.updateBatchById(vipUsers);
        return vipUsers;
    }
}
